<?php
/**
 * Created by PhpStorm.
 * User: MSQ
 * Date: 2021/9/24
 * Time: 14:27
 */

namespace App\Utils;


class DateHelp
{
    const DATE_FORMAT_1 = 'Y-m-d';// 2020-01-01
    const DATE_FORMAT_2 = 'Y-m-d H:i:s';// 2020-01-01 01:01:01
    const DATE_FORMAT_3 = 'Y-m';// 2020-01

    /**
     * 获取当前日期
     * @param string $format
     * @return string
     */
    public static function nowDate(string $format = self::DATE_FORMAT_2): string
    {
        return date($format);
    }

    /**
     * 时间戳转日期
     * @param int $time 时间戳
     * @param string $format 格式
     * @return string
     */
    public static function timestampToDate(int $time, string $format = self::DATE_FORMAT_2): string
    {
        return $time > 0 ? date($format, $time) : '';
    }

    /**
     * 获取当前毫秒级别的时间戳
     * @return float
     */
    public static function getMsecTime(): float
    {
        list($msec, $sec) = explode(' ', microtime());
        $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
        return $msectime;
    }

    /**
     * 获取今日开始时间戳和结束时间戳
     *
     * 语法：mktime(hour,minute,second,month,day,year) => (小时,分钟,秒,月份,天,年)
     */
    public static function today()
    {
        return [
            'start' => mktime(0, 0, 0, date('m'), date('d'), date('Y')),
            'end' => mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1,
        ];
    }
}
